<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     Pagizer
 * @subpackage  backoffice
 */

class m_backoffice_documents_Alias extends m_backoffice_documents_AliasBase
{
	private static $instances;

    private $aliasedDocument;

    public function __construct($id = null, $lang = null)
	{
		parent::__construct($id, $lang);

        $this->getAliasDocument();
	}

	/**
	 * @return m_backoffice_documents_Alias
	 */
	public static function createNew()
	{
		return new self();
	}

	/**
	 * @return m_backoffice_documents_Alias
	 */
	public static function createNewFromDocument($document)
	{
        $self = new self();
        $self   ->setLang($document->getLang())
                ->setLabel($document->getLabel())
                ->setAliasDocumentId($document->getUniqueId())
                ->setAliasDocumentModel($document->getModel())
                ->setAliasModule($document->getDocumentModuleName());
		return $self;
	}
	
	/**
	 * @return m_backoffice_documents_Alias
	 */
	public static function getInstanceByUniqueId($id, $lang = null)
	{        
		if(is_null($lang)) $lang = f_core_Context::getInstance()->getLang();
		
		if(!isset(self::$instances[$lang][$id]))
		{
			self::$instances[$lang][$id] = new self($id, $lang);		
		}
		return self::$instances[$lang][$id];
	}

    private function getAliasDocument()
    {
        if(!is_object($this->aliasedDocument) && !($this->aliasedDocument instanceof f_document_baseDocument))
        {
            if($this->isATranslation() && !is_null($this->getVO()))
            {

                if(!is_null($this->getAliasDocumentId()) && !is_null($this->getAliasDocumentModel()))
                {
                    $this->aliasedDocument = f_document_Provider::getInstance()->getByModelAndUniqueId($this->getAliasDocumentModel(), $this->getAliasDocumentId(), $this->getLang());
                }
                else
                {
                    $voAlias = $this->getVoDocument();
                    $this->aliasedDocument = f_document_Provider::getInstance()->getByModelAndUniqueId($voAlias->getAliasDocumentModel(), $voAlias->getAliasDocumentId(), $this->getLang());
                }
            }
            else
            {
                if(!is_null($this->getAliasDocumentId()) && !is_null($this->getAliasDocumentModel()))
                {
                    $this->aliasedDocument = f_document_Provider::getInstance()->getByModelAndUniqueId($this->getAliasDocumentModel(), $this->getAliasDocumentId(), $this->getLang());
                }
            }
        }

        return $this->aliasedDocument;
    }

    public function getOriginalDocument()
    {
        return $this->aliasedDocument;
    }

    public function getIcon()
    {
        return 'menu_undo.png';
    }

    public function getEditUrl()
    {
        return $this->aliasedDocument->getEditUrl();
    }

    public function getAliasEditUrl()
    {
        $configModule = $this->isATranslation() ? $this->getVoDocument()->getAliasModule() :$this->getAliasModule();
        return f_core_Routing::getUrl('genericEditAlias', array('configModule' => $configModule,'docId' => $this->getUniqueId(), 'docLang' => strtolower($this->getLang())));
    }

    public function getDeleteUrl()
    {
        return f_core_Routing::getUrl('deleteGeneric', array('moduleName' => $this->getDocumentModuleName(), 'docId' => $this->getUniqueId(), 'docLang' => strtolower($this->getLang())));
    }

    public function getLabel()
    {
        return $this->aliasedDocument->getLabel();
    }

	public function isTranslated()
	{
		return $this->aliasedDocument->getPublicationstatus() != f_document_baseDocument::NOTRANSLATION;
	}

    public function getPublicationStatus()
    {
        if(isset($this->aliasedDocument))
        {
            return $this->aliasedDocument->getPublicationStatus();
        }
        return parent::getPublicationstatus();
    }

    public function canSlide()
	{
		return true;
	}

	/**
	 * Magic PHP5 method to simulate add and get
	 *
	 * @param string $method
	 * @param mixed $attr
	 * @return mixed
	 */
	public function __call($method, $attr)
	{
        $aliasedDocument = $this->getAliasDocument();

        return $aliasedDocument->$method($attr);
	}
}